import regionAPI from '@/apis/region.js'
import storage from 'lib/good-storage/storage.js'

const TYPE = {
  GET_REGIONS: 'Get_Regions',
  SET_CURRENT_REGION: 'Set_Current_Region',
  SET_LOGIN_REGION: 'Set_Login_Region',
  SET_TOP_REGION: 'Set_Top_Region'
}

export default {
  state: {
    regions: [],
    currentRegionCode: '',
    loginRegionCode: '',
    topRegionCode: '',
    topRegion: ''
  },
  mutations: {
    [TYPE.GET_REGIONS] (state, payload) {
      state.regions = payload
    },
    [TYPE.SET_CURRENT_REGION] (state, payload) {
      state.currentRegionCode = payload
    },
    [TYPE.SET_LOGIN_REGION] (state, payload) {
      state.loginRegionCode = payload
    },
    [TYPE.SET_TOP_REGION] (state, payload) {
      state.topRegion = payload
      state.topRegionCode = payload
    }
  },
  actions: {
    getRegions (context, params, getRegionsComplete) {
      return new Promise((resolve, reject) => {
        try {
          regionAPI
            .getRegionsByApi(params)
            .then(res => {
              if (res && res.data) {
                const _data = res.data.data
                const op = {
                  apiCode: 'getRegion',
                  params: [
                    {
                      code: 'level',
                      value: 2
                    },
                    {
                      code: 'parent_code',
                      value: 'SD'
                    }
                  ]
                }
                regionAPI.getRegionsByApi(op).then(result => {
                  if (result && result.data) {
                    const _res = result.data.data
                    const frs = [..._data, ..._res]
                    context.commit(TYPE.GET_REGIONS, frs)
                    resolve(frs)
                  }
                })
              }
            })
            .catch(err => {
              reject(err)
            })
        } catch (err) {
          reject(err)
        }
      })
    },
    setCurrentRegion (context, code) {
      if (!code) {
        console.error('region is null')
        return
      }

      const getRegionByCode = code => {
        regionAPI
          .getCurrentRegionPrtChdRegs(code)
          .then(result => {
            if (result && result.length) {
              const regions = context.state.regions
              for (let i = 0; i < result.length; i++) {
                const _region = regions.find(r => r.code == result[i].code)
                if (!_region) {
                  const newitem = JSON.parse(JSON.stringify(result[i]))
                  regions.push(newitem)
                }
              }
              storage.set('all_regions', regions)
              context.commit(TYPE.GET_REGIONS, regions)

              const curRegionObj = regions.find(r => r.parentCode == code) || {}
              if (curRegionObj.level > 3) {
                getRegionByCode(curRegionObj.code)
              }
            }
          })
          .catch(err => {
            console.error(err)
          })
      }

      regionAPI
        .getCurrentRegionPrtChdRegs(code)
        .then(result => {
          if (result && result.length) {
            const regions = context.state.regions
            for (let i = 0; i < result.length; i++) {
              const _region = regions.find(r => r.code == result[i].code)
              if (!_region) {
                const newitem = JSON.parse(JSON.stringify(result[i]))
                regions.push(newitem)
              }
            }
            storage.set('all_regions', regions)
            context.commit(TYPE.GET_REGIONS, regions)
            window._adoPremise.regions = regions
            context.commit(TYPE.SET_CURRENT_REGION, code)
            window._adoPremise.currentRegion = code

            const curRegionObj = regions.find(r => r.parentCode == code) || {}
            if (curRegionObj.level > 3) {
              getRegionByCode(curRegionObj.code)
            }
          }
        })
        .catch(err => {
          console.error(err)
        })
    }
  }
}
